package Sever端;

public class SeverDeal {
    public static SQLDeal sqlDeal = new SQLDeal();

    public static String dealCmd(String cmd) {
        String[] str = getHeader(cmd);
        String header = str[0];
        cmd = str[1];
        //System.out.println("header:" + header+"  cmd:"+cmd);
        if ("login".equals(header)) return sqlDeal.check_account(cmd);
        else if ("register".equals(header)) return sqlDeal.add_account(cmd);
        else if ("create".equals(header)) return sqlDeal.create_group(cmd);
        else if ("get".equals(header)) return get(cmd);//统一处理获取类请求
        else if ("send".equals(header)) return send(cmd);//统一处理接受类请求
        //指令处理区
        return "error";
    }

    private static String get(String cmd) {//统一处理获取类请求
        int index = cmd.indexOf(' ');
        String type = cmd.substring(0, index);
        cmd = cmd.substring(index + 1);
        //System.out.println("type:"+type+"  cmd:"+cmd);
        return switch (type) {
            case "info" -> sqlDeal.get_info(cmd);
            case "friend" -> sqlDeal.get_friend(cmd);
            case "group" -> sqlDeal.get_member(cmd);
            case "in" -> sqlDeal.get_group(cmd);
            case "name" -> sqlDeal.get_name(cmd);
            default -> "error";
        };
    }

    private static String send(String cmd) {//统一处理接受类请求
        int index = cmd.indexOf(' ');
        String type = cmd.substring(0, index);
        cmd = cmd.substring(index + 1);
        //System.out.println("type:"+type+"  cmd:"+cmd);
        return switch (type) {
            case "info" -> sqlDeal.add_info(cmd);
            case "friend" -> sqlDeal.add_friend(cmd);
            case "group" -> sqlDeal.add_group(cmd);
            default -> "error";
        };
    }

    private static String[] getHeader(String cmd) {
        int start = cmd.indexOf('<');
        int end = cmd.indexOf('>');
        return new String[]{cmd.substring(start + 1, end).trim(), cmd.substring(end + 1).trim()};
    }
}